'use strict';
const { verifyToken } = require("wx-common")

exports.main = async (event, context) => {
	const { userInfo, token } = event
	
	const id = `wx${+ new Date()}`
	const db = uniCloud.database()
	const dbCmd = db.command
	
	const payload = verifyToken(token)
	
	// 带id的userinfo
	let wholeUserInfo = {...userInfo, ...{ id: id }}
	
	// 删除 _id 字段
	delete wholeUserInfo["_id"]
	
	const dbRes = await db.collection("users").where({
		openid: dbCmd.eq(payload.openid) 
	}).update(wholeUserInfo)
	
	//返回数据给客户端
	return {
		code: 200,
		data: wholeUserInfo
	}
};
